<?php
declare(strict_types=1);

namespace App\AdminApi\Modular\System\Controller;

use App\AdminApi\BasicsController;
use App\AdminApi\Modular\System\Search\SmsSearch;
use App\AdminApi\Modular\System\Validate\SmsValidate;
use App\Common\Extend\Guard\Auth;
use App\Common\Modular\System\Model\SmsTemplateModel;
use App\Common\Modular\System\Serve\{SmsServe, SmsTemplateServe};
use Core\HyperfUnit\Response;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\HyperfValidateAnnotations\Validate;
use Zms\Sms\Sms;

#[
    Auth(guard: self::GUARD, label: ['系统管理', '短信记录'], app: self::APP),
    Controller(prefix: "/admin-api/system-sms", server: self::SERVER)
]
class SmsController extends BasicsController
{
    #[Inject]
    protected SmsServe $server;

    #[Inject]
    protected SmsTemplateServe $template;

    #[
        GetMapping('list'),
        Auth(label: ['短信发送记录'], sign: 'system.sms', view: '/system/sms/index')
    ]
    public function list(): ResponseInterface
    {
        $search = new SmsSearch([
            'where' => $this->request->inputs(['phone', 'status', 'driver', 'template_id']),
            'order' => 'id DESC',
            'page' => $this->page(),
            'size' => $this->pageSize(),
            'column' => ['id', 'phone', 'template_id', 'driver', 'status', 'content', 'send_at']
        ]);
        return Response::Api()->success(data: $search->search());
    }


    #[
        GetMapping('list-exp'),
        Auth(label: ['导出发送记录'], sign: 'system.sms-exp')
    ]
    public function ListExp(): ResponseInterface
    {
        $search = new SmsSearch([
            'where' => $this->request->inputs(['phone', 'status', 'driver', 'template_id']),
            'order' => 'id DESC',
            'page' => 1,
            'size' => 30000,
            'column' => ['phone', 'driver','status', 'content', 'send_at']
        ]);
        return Response::Api()->success(data: $search->export("短信发送记录"));
    }


    #[
        GetMapping('template-list'),
        Auth(label: ['短信模板'], sign: 'system.sms.template-list')
    ]
    public function TemplateList(): ResponseInterface
    {
        return Response::Api()->success(data: $this->template->getSmsTemplateList());
    }

    #[
        PostMapping('update-template'),
        Auth(label: ['更新短信模板'], sign: 'system.sms.update-template'),
        Validate(SmsValidate::class)
    ]
    public function UpdateTemplate(): ResponseInterface
    {
        $key = $this->request->input('KEY');
        $template_id = $this->request->input('TEMPLATE_ID');
        $content = $this->request->input('CONTENT');

        /**
         * @var SmsTemplateModel $template
         */
        $template = $this->template->search(['key' => $key])->first();
        if (!$template) {
            $template = $this->template->getModel();
            $template->key = $key;
        }
        $template->template_id = $template_id;
        $template->content = $content;
        if ($template->save()) {
            return Response::Api()->success('短信模板设置成功');
        }
        return Response::Api()->fail('短信模板设置失败');
    }

    #[
        GetMapping('drives'),
        Auth(label: ['获取短信信息'], sign: 'skip'),
        Validate(SmsValidate::class)
    ]
    public function drives(): ResponseInterface
    {
        return Response::Api()->success(data: Sms::drives());
    }
}